package com.zhn.resourcej.web.request;

import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWTUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Value;
public class JWTInterceptors implements HandlerInterceptor {
    @Value("${jwt.secret}")
    private String secret;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        return true;
//        Map<String,Object> map = new ConcurrentHashMap<>();
//        // 获取请求头中令牌
//        String token = request.getHeader("token");
//        //如果不是映射到方法直接通过
//        if (!(handler instanceof HandlerMethod)){
//            return true;
//        }
//        //token有效放行
//        if(token != null && !token.isEmpty() && JWTUtil.verify(token,secret.getBytes())){
//            return true;
//        }
//        map.put("msg","token无效");
//        String json= JSONUtil.toJsonStr(map);
//        response.setStatus(401);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().println(json);
//        return false;
    }
}
